package com.meizhao.api.controller.demo;


import cn.hutool.db.Entity;
import com.meizhao.api.service.DbService;
import com.meizhao.api.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;

@RestController("testController")
public class TestController {

    private DbService dbService;

    @Autowired
    public TestController(DbService dbService){
        this.dbService = dbService;
    }

    @RequestMapping("/hello")
    public Object test(){
        Entity entity = Entity.create("account");
        entity.set("age","100");
        Entity entity1 = Entity.create("address");
        entity.set("address1","1");

        return (Integer)dbService.exec(session -> {
            try {
                session.insert(entity);
                session.insert(entity1);
                return 1;
            } catch (SQLException e) {
                e.printStackTrace();
                return -3;
            }
        }) == -3 ? ResultUtils.failed("Error") :
                ResultUtils.success("Success");
    }


}
